package com.example.administrator.testproject.MVP.Recilte;

import com.example.administrator.testproject.MVP.Entity.DetectListItemDto;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class RetileMain {
	
	private String detectMainUrl = "http://jandan.net/";
	private List<DetectListItemDto> detectUrlList = new ArrayList<>();
	
	public static void main(String[] arg){
		RetileMain retile = new RetileMain();
	
		try {
			retile.doDetectJandanHomePageData(null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public List<DetectListItemDto> getDetectUrlList() {
		return detectUrlList;
	}

	public interface simpleListener{
		void onListener();
		void onDetailListener(String str);
	}

	public void doDetectJandanHomePageData(final simpleListener listener) throws Exception{

		new Thread(new Runnable() {
			@Override
			public void run() {
				Connection conn = Jsoup.connect(detectMainUrl)
						.userAgent("Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
				Document doc = null;
				try {
					doc = conn.get();
					Element content = doc.getElementById("content");
					Elements posts = content.getElementsByClass("list-post");
					for(Element post : posts){
						DetectListItemDto itemDot = new DetectListItemDto();
						Elements thumbs_b = post.getElementsByClass("thumbs_b");
						Elements img = thumbs_b.select("img");
						String imgPath = img.attr("src");
						if("".equals(imgPath)){
							imgPath = img.attr("data-original");
						}
						itemDot.setItemImgUrl("http:" + imgPath);

						Elements indexs = post.getElementsByClass("indexs");
						Elements as = indexs.select("a");
						itemDot.setItemUrl(as.get(0).attr("href"));
						itemDot.setTitle(as.get(0).text());
						itemDot.setAuthor(as.get(1).text());
						itemDot.setAuthorPageUrl(as.get(1).attr("href"));
						Elements strong_a = indexs.select("strong").select("a");
						itemDot.setTab(strong_a.text());
						itemDot.setTabPlateUrl(strong_a.attr("href"));
						itemDot.setSketch(indexs.text());
						detectUrlList.add(itemDot);
					}
					if(listener != null)
						listener.onListener();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	public void doDetectJandanDetailPageData(final String url, final simpleListener listener) throws Exception{

		new Thread(new Runnable() {
			@Override
			public void run() {
				Connection conn = Jsoup.connect(url)
						.userAgent("Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
				Document doc = null;
				try {
					doc = conn.get();
					Element content = doc.getElementById("content");
					String contentStr = content.getElementsByTag("p").text();
					if(listener != null)
						listener.onDetailListener(contentStr);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
}
